package com.javasm.handler;

import com.alibaba.fastjson.JSON;
import com.javasm.domain.ResponseResult;
import com.javasm.utlis.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author：MoDebing
 * @Version：1.0
 * @Date：2022-09-02-11:02
 * @Description: 授权失败处理
 */
//@Component
public class AuthenticationDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        ResponseResult result = new ResponseResult(HttpStatus.FORBIDDEN.value(),"您的权限不足");
        String json = JSON.toJSONString(result);
        // 处理异常
        WebUtils.renderString(httpServletResponse,json);
    }
}
